'''

Case Type： 数据库系统
Case Name： 将pg_log目录权限恢复，重启数据库成功，业务可正常运行
Create At:  2020/06/23
@zou_jialiang0505328126
Descption:  1.恢复pg_log权限2.重启数据库3.查看TPCC业务是否被影响

history：
created： 2020/06/23  created by @wan005
'''
import os
import unittest
import sys
from yat.test import macro
from yat.test import Node

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common


class ModifyPglogPermi(unittest.TestCase):
    sh = CommonSH()
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case043.py start------------')
        self.rootNode = Node()
        self.dbUserNode = Node(node='dbuser')
        self.TPCC_PATH = macro.TPCC_PATH
        self.PG_LOG_PATH = macro.PG_LOG_PATH
        self.com = Common()

        self.log.info("delete pg_log permission")
        modfiy_permis_cmd = 'chmod -R 000 ' + self.PG_LOG_PATH
        modify_msg = self.rootNode.sh(modfiy_permis_cmd).result()
        self.assertEqual(modify_msg, '')

    def test_repermission_pglog(self):
        self.log.info('recover pg_log permission')
        modfiy_permis_cmd = 'chmod -R 700 ' + self.PG_LOG_PATH
        modify_msg = self.rootNode.sh(modfiy_permis_cmd).result()
        self.assertEqual(modify_msg, '')
        show_permission_cmd = 'ls -al ' + self.PG_LOG_PATH
        show_msg = self.rootNode.sh(show_permission_cmd).result()
        self.log.info(show_msg)

        self.log.info('start opengauss')
        is_started = self.sh.startDbCluster()

        self.assertTrue(is_started)

        # startTPCC
        tpcc_result = self.com.startTPCC(self.rootNode, self.TPCC_PATH)
        result = tpcc_result.find("Session End") > -1
        self.log.info(tpcc_result)
        self.assertTrue(result)

    def tearDown(self):
        modfiy_permis_cmd = 'chmod -R 700 ' + self.PG_LOG_PATH
        self.log.info(modfiy_permis_cmd)
        modify_msg = self.rootNode.sh(modfiy_permis_cmd).result()
        self.log.info(modify_msg)
        self.log.info('this is tearDown')
